package xyz.thoughtset.viewer.apis.client.controller;


import cn.idev.excel.ExcelWriter;
import cn.idev.excel.FastExcel;
import cn.idev.excel.write.metadata.WriteSheet;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.thoughtset.viewer.common.core.constants.FolderNameConstant;
import xyz.thoughtset.viewer.executor.blocks.executor.BlockExecutorManager;
import xyz.thoughtset.viewer.modules.api.controller.BaseApiController;
import xyz.thoughtset.viewer.modules.api.entity.ApiInfo;
import xyz.thoughtset.viewer.modules.api.service.ApiInfoService;
import xyz.thoughtset.viewer.modules.excel.entity.ExportDataInfo;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/t/")
public class TestFunQueryController extends BaseApiController {
    @Autowired
    protected BlockExecutorManager blockExecutorManager;

    @GetMapping(value = "{subject}")
    @ResponseBody
    public Object query(@RequestParam(required = false) LinkedHashMap<String, Object> searchMap
            , @PathVariable("subject") String subject) {
        return blockExecutorManager.execBlocks(subject, searchMap);
    }


//    @GetMapping(value = "e/{subject}")
//    @ResponseBody
//    public void excel(@RequestParam(required = false) LinkedHashMap<String, Object> searchMap
//            , @PathVariable("subject") String subject, HttpServletResponse response) {
//        ExportDataInfo exportDataInfo = exportDataService.getById(subject);
//        if (exportDataInfo == null) {
//            throw new NullPointerException("exportData not found");
//        }
//        Map<String, Object> data = (Map<String, Object>) query(searchMap, exportDataInfo.getBodyId());
//        String fileName = exportDataInfo.getFileName();
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        response.setCharacterEncoding("utf-8");
////        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
//        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
//
//        try (
//                InputStream templateStream = new FileInputStream(
//                        FolderNameConstant.folderWithSep(FolderNameConstant.TEMPLATE,exportDataInfo.getTemplateId()));
//                ExcelWriter writer =
//                         FastExcel.write(response.getOutputStream())
//                                 .withTemplate(templateStream).build()) {
//            WriteSheet writeSheet = FastExcel.writerSheet().build();
//
//            for (Map.Entry<String, Object> entry : data.entrySet()) {
//                String key = entry.getKey();
//                Object value = entry.getValue();
//                writer.fill(value, writeSheet);
//            }
//            // 填充列表数据，开启 forceNewRow
////            FillConfig config = FillConfig.builder().forceNewRow(true).build();
////            writer.fill(data, config, writeSheet);
//
//            // 填充普通变量
////            Map<String, Object> map = new HashMap<>();
////            map.put("date", "2024年11月20日");
////            map.put("total", 1000);
////            writer.fill(map, writeSheet);
//            writer.finish();
//        }catch (Exception e) {
//            throw new RuntimeException(e.getMessage(), e);
//        }
//    }


}
